import { ElMessage, ElMessageBox } from 'element-plus'
import nProgress from 'nprogress'
// 提示成功
export function toast(message, type = "success",dangerouslyUseHTMLString = true) {
    ElMessage({
        message,
        type,
        dangerouslyUseHTMLString,
        duration: 3000
    })
}

export function showModal(content = "提示内容", type = "warning", title = "") {
    return ElMessageBox.confirm(
        content,
        title,
        {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type,
        }
    )
}

// 显示全屏loading
export function showFullLoading(content = "提示内容", type = "warning", title = "") {
    nProgress.start()
}
// 隐藏全屏loading
export function hideFullLoading(content = "提示内容", type = "warning", title = "") {
    nProgress.done()
}

// 弹出输入框
export function showPrompt(tip, value=""){
    return ElMessageBox.prompt(tip, 'Tip', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        inputValue: value
      })
}

// 将query对象转成url参数
export function queryParams(query){
    let q = []
    for(const key in query){
        if(query[key]){
            q.push(`${key}=${encodeURIComponent(query[key])}`)
        }
    }
    let r = q.join("&")
    r = r ? "?"+r : ""
    return r
}

// 上移
export function useArrayMoveUp(arr,index){
    swapArray(arr,index,index-1)
}

// 下移
export function useArrayMoveDown(arr,index){
    swapArray(arr,index,index+1)
}

function swapArray(arr,index1,index2){
    arr[index1] = arr.splice(index2,1,arr[index1])[0]
    return arr
}